<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>兑换管理</title>
    <script type="text/javascript" src="/js/jquery.js"></script>
</head>
<body>

<ul id="J_list">


</ul>

<script>
    var page = 1;
    var rows = 10;
    $(function(){
        getExchangeList();
        //打开验证码
        $(".verify").click(function(){
           var id = $(this).attr("data-id");
           var code = $(this).attr("data-code");
           console.log(code,"code");
        });
        //收货
        $(".delivery").click(function(){
            var id = $(this).attr("data-id");
            if(confirm("确定要确认收货")){
                var data = {id:id};
                var url = "/exchange/finishExchange";
                $.ajax({
                    type: "POST",
                    dataType: 'json',
                    async: false,
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(data),
                    url: url,
                    success: function(res){
                        if(res.code = "200"){
                            //收货成功
                            alert("提交成功");
                            page = 1;
                            getExchangeList();
                        }else{
                            alert(res.msg);
                        }
                    },
                    error: function(e){

                    },
                    complete: function(e){

                    }
                });

            }
        });


    });


    function getExchangeList(){
        var uid = "${uid}";
        var data = {page:page,rows:rows,uid:uid};
        var url = "/exchange/user_exchange";
        $.ajax({
            type: "POST",
            dataType: 'json',
            async: false,
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(data),
            url: url,
            success: function(res){
                if(res.code = "200"){
                    //操作
                    var list = res.data.list;
                    for (var i =0; i<list.length;i++){
                        var shtml = "";
                        shtml += '<li><div>兑换单号:'+list[i]['order_no']+'</div> ';
                        if(list[i]['type'] == 1 && list[i]['status'] == 1){
                            shtml += '<div>待核销</div>';
                        }else if(list[i]['type'] == 1 && list[i]['status'] == 2){
                            shtml += '<div>已核销</div>';
                        }else if(list[i]['type'] == 2 && list[i]['status'] == 1){
                            shtml +=  '<div>已发货</div>';
                        }else if(list[i]['type'] == 2 && list[i]['status'] == 2){
                            shtml +=  '<div>已收货</div>';
                        }
                        shtml += '<div id="'+list[i]['id']+'"><img src="'+list[i]['pic']+'" width="100px" height="100px" border="1px solid #000" alt=""><div>'+list[i]['title']+'</div></div>';
                        if(list[i]["type"] == 0){
                            shtml += '<button class ="verify" data-id="'+list[i]['id']+'" data-code="'+list[i]['verify_code']+'">核销码</button>';
                        }else{
                            shtml += '<button class ="delivery" data-id="'+list[i]['id']+'">收货</button>';
                        }
                        $("#J_list").append(shtml);
                    }
                }else{
                    alert(res.msg);
                }
            },
            error: function(e){

            },
            complete: function(e){

            }
        });

    }
</script>

</body>
</html>
